import { Math } from 'core-js'
import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
export default createStore({
  state: {
    // colors:['blue','red','green'], // 主题颜色
    useColor: 'red',
    books:[], // 书架的书
  },
  mutations: {
    cutColor(state){ // 切换颜色
      // let num = parseInt(Math.random()*3); // Math.random(); 是0-1的随机数
      // '#' + Math.random().toString(16).substr(2, 6).toUpperCase();
      state.useColor = '#' + Math.random().toString(16).substr(2, 6).toUpperCase(); // toString(n) n 就代表进制数 // toUpperCase 转换成大写
      // console.log(Math.random().toString(16));
    },
    addBook(state,val){
      let bool = state.books.some(item => item.id == val.id);
      if (bool){
        alert('书架里面已有此书！')
      } else {
        state.books.push(val);
      }
    },
    cutState(state,i){
      state.books[i].state = state.books[i].state==1?0:1
    },
    del(state,i){ // 书籍的删除
      state.books.splice(i,1);
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    new persist({
      storage: window.localStorage,
    }),
  ]
})
